package tech.lq0.dreamaticvoyage.block.entity;

import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;
import tech.lq0.dreamaticvoyage.capability.ModCapabilities;
import tech.lq0.dreamaticvoyage.gui.menu.FukamizuPylonMenu;
import tech.lq0.dreamaticvoyage.gui.slot.ContainerEnergyData;
import tech.lq0.dreamaticvoyage.init.BlockEntityRegistry;

import java.util.concurrent.atomic.AtomicInteger;

public class FukamizuPylonBlockEntity extends AbstractPylonBlockEntity implements MenuProvider {

    public static final int MAX_RANGE = 16;
    public static final int MAX_ENERGY = 30000;
    public static final int CHARGE_SPEED = 32;
    public static final int TRANSFER_COOLDOWN = 40;
    public static final int MAX_CONNECT_COUNT = 10;
    public static final int MAX_DATA_COUNT = 1;

    protected final ContainerEnergyData dataAccess = new ContainerEnergyData() {
        public long get(int pIndex) {
            if (pIndex == 0) {
                AtomicInteger energy = new AtomicInteger();
                FukamizuPylonBlockEntity.this.getCapability(ModCapabilities.UMISU_CURRENT_ENERGY_CAPABILITY).ifPresent(consumer -> energy.set(consumer.getEnergyStored()));
                return energy.get();
            }
            return 0;
        }

        public void set(int pIndex, long pValue) {
            if (pIndex == 0) {
                FukamizuPylonBlockEntity.this.getCapability(ModCapabilities.UMISU_CURRENT_ENERGY_CAPABILITY).ifPresent(consumer -> consumer.receiveEnergy((int) pValue, false));
            }
        }

        public int getCount() {
            return MAX_DATA_COUNT;
        }
    };

    public FukamizuPylonBlockEntity(BlockPos pPos, BlockState pBlockState) {
        super(BlockEntityRegistry.FUKAMIZU_PYLON_BLOCK_ENTITY.get(), pPos, pBlockState,
                new Builder().range(MAX_RANGE).capacity(MAX_ENERGY).chargeSpeed(CHARGE_SPEED)
                        .transferCooldown(TRANSFER_COOLDOWN).maxConnectCount(MAX_CONNECT_COUNT));
    }

    public static void serverTick(Level pLevel, BlockPos pPos, BlockState pState, FukamizuPylonBlockEntity pylonBlockEntity) {
        pylonBlockEntity.selfCharge(pLevel, pPos, pState);
        pylonBlockEntity.refreshConnections(pLevel, pPos);
        pylonBlockEntity.transferEnergy(pLevel, pPos);
    }

    public static void clientTick(Level pLevel, BlockPos pPos, BlockState pState, FukamizuPylonBlockEntity pylonBlockEntity) {
        pylonBlockEntity.refreshConnections(pLevel, pPos);
    }

    @Override
    public Component getDisplayName() {
        return Component.translatable("container.dreamaticvoyage.fukamizu_pylon");
    }

    @Nullable
    @Override
    public AbstractContainerMenu createMenu(int pContainerId, Inventory pPlayerInventory, Player pPlayer) {
        return new FukamizuPylonMenu(pContainerId, pPlayerInventory, this, this.dataAccess);
    }

    @Override
    public int getPylonLevel() {
        return 1;
    }
}
